#!/bin/bash
# author    tyb

# 函数
function isgreen() {
    if [ $? -eq 0 ]; then
        echo -e "\e[32m$1==========>succeed\e[0m"
    else
        echo -e "\e[31m$1==========>failed\e[0m"
        exit 1
    fi
}

function check_master() {
    local host="$1"
    if ping -c 1 "$host" > /dev/null 2>&1; then
        echo -e "\e[32mmaster: \"$host\" is available.\e[0m"
    else
        echo "$host 无法 ping 通."
        exit 1
    fi
}

function check_passed() {
    # 安装sshpass
    if ! rpm -q sshpass >/dev/null 2>&1; then
        echo "安装sshpass"
        yum install -y sshpass >/dev/null 2>&1
        isgreen "安装sshpass"
    fi
    local passwd="$1"
    local host="$2"
    if sshpass -p "$passwd" ssh  -o StrictHostKeyChecking=no  root@"$host" "echo Successful login" >/dev/null 2>&1; then
        echo -e "\e[32mmaster: password is available.\e[0m"
    else
        echo -e "\e[31mpassword is wrong\e[0m"
        exit 1
    fi
}

function show_help() {
    echo "用法: $(basename "$0") [选项] <参数>"
    echo "选项:"
    echo "  --master <主机>         检测指定主机是否可达"
    echo "  --password <passwd>     检测指定主机的密码是否正确"
    echo "  --hostname <host>       Specify the hostname. dedfault: worker"
    echo "  -h, --help              显示帮助信息"
    exit 0
}

# 如果没有参数则显示帮助信息并退出
if [ $# -eq 0 ]; then
    show_help
fi



# 获取master节点的信息
master=""
passwd=""
while [[ $# -gt 0 ]]; do
  case "$1" in
    --master)
        if [[ -n "$2" ]]; then
          check_master "$2"
          master="$2"
          shift 2
        else
          echo "Missing argument for --master option."
          exit 1
        fi
        ;;
    --password)
        if [[ -n "$2" ]]; then
            check_passed "$2" "$master"
            passwd="$2"
            shift 2
        else
            echo "Missing argument for --password option."
            exit 1
        fi
        ;;
    --hostname)
      if [[ -n "$2" ]]; then
        host="$2"
        shift 2
      else
        echo "Missing argument for -h option."
        exit 1
      fi
      ;;
    -h|--help)
        show_help
        ;;
    *)
        echo "Invalid option: $1"
        exit 
        ;;
  esac
done

if [[ -z "$master" || -z "$passwd" ]]; then
    echo "Both --master and --password options are required."
    exit 1
fi

# 全局变量
hostname=${host:-"worker"}
ip=$(ip addr show eth0 | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | awk -F "/" '{print $1}')
version=$(sshpass -p "$passwd" ssh  -o StrictHostKeyChecking=no  root@"$master" "kubelet --version" | awk '{print $2}' | awk '{gsub("v", ""); print}')

# 修改主机名为获取到的主机名
echo "修改主机名"
hostnamectl set-hostname "$hostname"
isgreen "修改主机名为：$hostname"

# 配置hosts解析文件
echo "配置hosts解析"
if grep -q "$hostname" /etc/hosts; then
    #解析存在进行修改
    sed -i "s/.*$hostname.*/$ip $hostname/g" /etc/hosts
    ping "$hostname" -c 1 >/dev/null 2>&1
    isgreen "配置hosts解析"
else
    # 解析不存在就新添加
    echo "$ip $hostname" >>/etc/hosts
    ping "$hostname" -c 1 >/dev/null 2>&1
    isgreen "配置hosts解析"
fi

# 安装kubelet、kubeadm、kubectl

echo "安装kubelet、kubeadm、kubectl"
if ! rpm -q kubelet-"$version"-0.x86_64 >/dev/null 2>&1; then
    yum install -y kubelet-"$version"-0.x86_64 kubectl-"$version"-0.x86_64 kubeadm-"$version"-0.x86_64 >/dev/null 2>&1
    isgreen "安装kubelet kubeadm kubectl"
else
    echo >/dev/null
    isgreen "安装kubelet kubeadm kubectl"
fi
systemctl enable kubelet --now >/dev/null 2>&1
isgreen "启动kubelet"

# 获取加入集群的命令
command=$(sshpass -p "$passwd" ssh  -o StrictHostKeyChecking=no  root@"$master" "kubeadm token create --print-join-command")"--ignore-preflight-errors=SystemVerification"

if [ -n "$command" ]; then
    echo "加入集群"
    $command
    isgreen "加入集群"
else
    echo "获取加入命令失败"
fi 

# 查看集群状态
echo "查看集群状态"
sshpass -p "$passwd" ssh  -o StrictHostKeyChecking=no  root@"$master" "kubectl get nodes"